# -*- coding: utf-8 -*-
# ###########################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv, fields
from tools.translate import _


class init_purchase_order(osv.osv):
    _inherit = 'purchase.order'

    _columns = {
        'import_export_date': fields.date('Fecha del Levante',
                                          states={'confirmed': [('readonly', True)], 'approved': [('readonly', True)]}),
        'tracking': fields.char('Ubicación', 255,
                                states={'confirmed': [('readonly', True)], 'approved': [('readonly', True)]}),
        'origin_country': fields.char('País de Origen', 255,
                                      states={'confirmed': [('readonly', True)], 'approved': [('readonly', True)]}),
        'current_rate': fields.float('Tipo de Cambio',
                                     states={'confirmed': [('readonly', True)], 'approved': [('readonly', True)]}),
        'import_state': fields.selection([('rojo', 'ROJO'), ('amarillo', 'AMARILLO'), ('verde', 'VERDE')],
                                         'Estado del Aforo',
                                         states={'confirmed': [('readonly', True)], 'approved': [('readonly', True)]}),
    }

    def create(self, cr, uid, vals, context=None):
        if context is None:
            context = {}

        if not 'partner_address_id' in vals.keys() and 'partner_id' in vals.keys():
            data_partner = self.onchange_partner_id(cr, uid, [], vals['partner_id'])['value']
            vals.update(data_partner)
        if not 'location_id' in vals.keys() and 'partner_id' in vals.keys():
            location_id = self.pool.get('res.partner').browse(cr, uid, vals['partner_id']).property_stock_customer.id
            vals.update({'location_id': location_id})
        return super(init_purchase_order, self).create(cr, uid, vals, context)


init_purchase_order()


class init_purchase_order_line(osv.osv):
    _inherit = 'purchase.order.line'

    def create(self, cr, uid, vals, context=None):
        if context is None:
            context = {}

        if not 'name' in vals.keys() and 'product_id' in vals.keys():
            name = self.pool.get('product.product').browse(cr, uid, vals['product_id']).name
            vals.update({'name': name})
        if not 'taxes_id' in vals.keys() and 'product_id' in vals.keys():
            taxes_id = self.pool.get('product.product').browse(cr, uid, vals['product_id']).taxes_id
            vals.update({'taxes_id': taxes_id})
        return super(init_purchase_order_line, self).create(cr, uid, vals, context)


init_purchase_order_line()
